package de.eridea.learning.usermanagement.webinterface;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;

import de.eridea.learning.java.usermanagement.logic.UserService;
import de.eridea.learning.java.usermanagement.logic.exception.InsufficientDataException;
import de.eridea.learning.java.usermanagement.logic.exception.MalformedDataException;
import de.eridea.learning.java.usermanagement.logic.exception.NameAlreadyExistsException;
import de.eridea.learning.java.usermanagement.logic.exception.NoSuchGroupException;
import de.eridea.learning.java.usermanagement.logic.exception.NoSuchUserException;
import de.eridea.learning.java.usermanagement.logic.exception.NothingToDoException;
import de.eridea.learning.java.usermanagement.logic.to.GroupData;
import de.eridea.learning.java.usermanagement.logic.to.UserDataEditable;

public class EditUserHandler {
	private UserBean userBean;
	private UserService userService;

	public String updateUserData() {
		if(userBean == null) {
			throw new IllegalStateException("userBean is null");
		}
		if(userService == null) {
			throw new IllegalStateException("userService is null");
		}
		try {
			UserDataEditable data = userService.getEditableUser(userBean.getLoginName());
			data.setDisplayName(userBean.getDisplayName());
			userService.updateUser(data);
		} catch (NoSuchUserException e) {
			throw new RuntimeException(e.getMessage(), e);
		} catch (InsufficientDataException e) {
			throw new RuntimeException(e.getMessage(), e);
		} catch (MalformedDataException e) {
			throw new RuntimeException(e.getMessage(), e);
		} catch (IllegalArgumentException e) {
			throw new RuntimeException(e.getMessage(), e);
		} catch (NameAlreadyExistsException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
		return "user-settings";
	}
	
	public String updateGroups() {
		if(userBean == null) {
			throw new IllegalStateException("userBean is null");
		}
		if(userService == null) {
			throw new IllegalStateException("userService is null");
		}
		String result;
		if(userBean.isLoggedIn()) {
			try {
				Set<GroupData> groups = userService.getUser(userBean.getLoginName()).getGroups();
				Collection<String> groupNames = new ArrayList<String>(groups.size());
				for (GroupData group : groups) {
					groupNames.add(group.getGroupName());
				}
				for(String groupName : userBean.getGroups()) {
					if(groupNames.contains(groupName)) {
						groupNames.remove(groupName);
					} else {
						userService.addUserToGroup(userBean.getLoginName(), groupName);
					}
				}
				for (String groupName : groupNames) {
					userService.removeUserFromGroup(userBean.getLoginName(), groupName);
				}
			} catch (NoSuchUserException e) {
				throw new RuntimeException(e.getMessage(), e);
			} catch (NothingToDoException e) {
				throw new RuntimeException(e.getMessage(), e);
			} catch (NoSuchGroupException e) {
				throw new RuntimeException(e.getMessage(), e);
			}
			result = "user-settings";
		} else {
			result = "not-logged-in";
		}
		return result;
	}

	public String[] getAllGroups() {
		Set<GroupData> groups = userService.getGroups();
		String[] groupNames = new String[groups.size()];
		{
			int k = 0;
			for (GroupData group : groups) {
				groupNames[k++] = group.getGroupName();
			}
		}
		return groupNames;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
